<?php

namespace App\Http\Controllers\Admin\Information;

use App\Repository\Information\InformationOrdersRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as BaseController;
use App\Repository\Product\GoodsRemarkRepository;

// 分销商
class GoodsOrderController extends BaseController
{
  private $repoist;
  public function __construct(InformationOrdersRepository $model)
  {
    $this->repoist = $model;
  }
  public function store(Request $request)
  {
    return $this->repoist->add($request);
  }
  public function index(Request $request)
  {
    $where = [];
    if (!empty($request->get('phone'))) {
      $where['where']['phone'] = $request->get('phone');
    }
    if (!empty($request->get('name'))) {
      $where['where']['name'] = $request->get('name');
    }
    if (!empty($request->get('idcard'))) {
      $where['where']['idcard'] = $request->get('idcard');
    }
    if (!empty($request->get('sup_id'))) {
      $where['In']['sup_id'] = $request->get('sup_id');
    }
    if (($request->has('page_id')) && $request->get('page_id') != '') {
      $where['In']['page_id'] =  $request->get('page_id');
    }
    if (($request->has('status')) && ($request->get('status') != '') && ($request->get('status') != -1)) {
      $where['In']['status'] = $request->get('status');
    }
    if (!empty($request->get('dateRange'))) {
      $time = $request->get('dateRange');
      $where['between'] = ['created_at' => [$time]];
    }
    $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
    $where['limit']['number'] = $request->get('pageSize');
    if ($request->user->role_id != 1) {
      $where['where']['shop_id'] = $request->user->shop_id;
    }
    // var_dump($where);
    $data =  $this->repoist->index($where);
    return $data;
  }
  public function save(Request $request)
  {
    if (!$request->get('id')) {
      return $this->returnMsg(1, '请传递id参数');
    }
    $where = ['id' => $request->get('id')];
    if ($request->user->role_id != 1) {
      $where['shop_id'] = $request->user->shop_id;
    }

    return $this->repoist->edit($where, $request);
  }
  public function  show(Request $request, $id)
  {
    $where = ['id' => $id];
    if ($request->user->role_id != 1) {
      $where['shop_id'] = $request->user->shop_id;
    }
    // var_dump($where);
    //查询一条数据
    $return = $this->repoist->getOne($where);
    if (!empty($return)) {
      // $return = usfirstArr($return);
      // 将文本模式 格式化
      if (!empty($return->detail)) {
        $return->detail = html_entity_decode($return->detail);
      }
      if (!empty($return->remark)) {
        $return->remark = html_entity_decode($return->remark);
      }
    }
    return $this->returnMsg(200, '查询成功', $return);
  }

  public function listss(Request $request)
  {
    $where = [];
    if (!empty($request->get('name'))) {
      $where['where'] = ['name' => $request->get('name')];
    }
    $where['where']['shop_id'] = $request->user->shop_id;
    // var_dump($where);
    if (!empty($request->get('select'))) {
      $where['select'] = ['name', 'id'];
    }
    $return = $this->repoist->lists($where, 1, 99999);
    return $return;
  }

  public function destroy(Request $request, $id)
  {
    if (!$id) {
      return $this->returnMsg(1, '请传递参数');
    }
    $where = [
      'id' => $id,
      'shop_id' => $request->user->shop_id
    ];
    return $this->repoist->del($where);
  }
  public function updateStatus(Request $request, $id)
  {
    $where = [
      'id' => $id,
      'shop_id' => $request->user->shop_id
    ];
    // 需要修改两个地方 一个是状态 一个 app是否上架
    $update = [
      'status' => $request->get('status'),
    ];

    return $this->repoist->edit($where, $request);
  }
  public function getStatus()
  {
    $arr =  $this->repoist->getStatus();
    $return = [];
    foreach ($arr as $key => $value) {
      $return[] = [
        'id' => $key,
        'name' => $value
      ];
    }
    return $this->returnMsg(200, '查询成功', $return);
  }
}
